<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="商品名称" prop="name">-->
<!--        <el-input-->
<!--          v-model="queryParams.name"-->
<!--          placeholder="请输入商品名称"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      -->
<!--      <el-form-item label="商品明细" prop="detail">-->
<!--        <el-input-->
<!--          v-model="queryParams.detail"-->
<!--          placeholder="请输入商品明细"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="商品类型" prop="type">-->
<!--        <el-select v-model="queryParams.type" placeholder="请选择商品类型" clearable size="small">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="发货时间" prop="sendTime">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.sendTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择发货时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="快递公司" prop="courierCompany">
        <el-input
          v-model="queryParams.courierCompany"
          placeholder="请输入快递公司"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运单号" prop="courierNumber">
        <el-input
          v-model="queryParams.courierNumber"
          placeholder="请输入运单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择商品类型" clearable size="small">
          <el-option
            v-for="dict in orderStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['commodityOrder:commodityOrder:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['commodityOrder:commodityOrder:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['commodityOrder:commodityOrder:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--		  :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['commodityOrder:commodityOrder:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->

<!--    </el-row>-->

    <el-table v-loading="loading" :data="commodityOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="序号" align="center" prop="id" />-->
      <el-table-column label="订单号" align="center" prop="number" width="200px"/>
      <el-table-column label="商品名称" align="center" prop="name" />
<!--      <el-table-column label="商品图片" align="center" prop="imgUrl" >-->
<!--        <template slot-scope="scope">-->
<!--          <el-image v-if="scope.row.imgUrl"-->
<!--                    style="width: 100px; height: 50px;border-radius: 10%;"-->
<!--                    :src="scope.row.imgUrl">-->
<!--          </el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="商品价格" align="center" prop="price" />
      <el-table-column label="商品原价" align="center" prop="originalPrice" />
      <el-table-column label="商品明细" align="center" prop="detail" />
      <el-table-column label="优惠券面值" align="center" prop="nominal" />
      <el-table-column label="商品类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="兑换数量" align="center" prop="count" />
      <el-table-column label="收件人" align="center" prop="userName" >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleAddressShow(scope.row)"
          >{{scope.row.userName}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="sendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="快递公司" align="center" prop="courierCompany" >
        <template slot-scope="scope">
          <span v-if="scope.row.courierCompany===null && scope.row.type===2 ">/</span>
          <span v-else>{{scope.row.courierCompany}}</span>
        </template>
      </el-table-column>
      <el-table-column label="运单号" align="center" prop="courierNumber" >
        <template slot-scope="scope">
          <span v-if="scope.row.courierNumber===null && scope.row.type===2 ">/</span>
          <span v-else>{{scope.row.courierNumber}}</span>
        </template>
      </el-table-column>

      <el-table-column label="订单状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['commodityOrder:commodityOrder:edit']"
          >添加发货信息</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['commodityOrder:commodityOrder:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="收件人信息" :visible.sync="addressOpen" width="30%" >
      <el-form ref="form" :model="addressForm" :rules="rules" label-width="100px" :disabled="true">
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item label="收件人" prop="userName">
              <el-input v-model="addressForm.userName"  :readonly="true" >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="addressForm.phone"  :readonly="true" >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item label="邮编" prop="areaCode">
              <el-input v-model="addressForm.areaCode"  :readonly="true" >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item label="地址" prop="area">
              <el-input type="textarea" v-model="addressForm.area"  :readonly="true" >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item label="详细地址" prop="detail1">
              <el-input type="textarea" v-model="addressForm.detail1"  :readonly="true" >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- 添加或修改积分兑换商品
对话框 -->
    <el-dialog title="添加快递信息" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="商品名称" prop="name">-->
<!--          <el-input v-model="form.name" placeholder="请输入商品名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="商品图片" prop="imgUrl">-->
<!--          <el-input v-model="form.imgUrl" placeholder="请输入商品图片" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="商品价格" prop="price">-->
<!--          <el-input v-model="form.price" placeholder="请输入商品价格" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="商品原价" prop="originalPrice">-->
<!--          <el-input v-model="form.originalPrice" placeholder="请输入商品原价" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="商品明细" prop="detail">-->
<!--          <el-input v-model="form.detail" placeholder="请输入商品明细" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="优惠券面值" prop="nominal">-->
<!--          <el-input v-model="form.nominal" placeholder="请输入优惠券面值" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="商品类型" prop="type">-->
<!--          <el-select v-model="form.type" placeholder="请选择商品类型">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->

        <el-form-item label="快递公司" prop="courierCompany">
          <el-input v-model="form.courierCompany" placeholder="请输入快递公司" />
        </el-form-item>
        <el-form-item label="运单号" prop="courierNumber">
          <el-input v-model="form.courierNumber" placeholder="请输入运单号" />
        </el-form-item>
<!--        <el-form-item label="发货时间" prop="sendTime">-->
<!--          <el-date-picker clearable size="small"-->
<!--                          v-model="form.sendTime"-->
<!--                          type="date"-->
<!--                          value-format="yyyy-MM-dd"-->
<!--                          placeholder="选择发货时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="订单状态">-->
<!--          <el-radio-group v-model="form.status">-->
<!--            <el-radio label="1">请选择字典生成</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommodityOrder, getCommodityOrder, delCommodityOrder, addCommodityOrder, updateCommodityOrder, exportCommodityOrder } from "@/api/commodity/commodityOrder";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "CommodityOrder",
  components: {
    ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 积分兑换商品表格数据
      commodityOrderList: [],
      //字典
      commodityTypeOptions: [],
      orderStatusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      addressOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        number: null,
        userId: null,
        addressId: null,
        commodityId: null,
        couponId: null,
        name: null,
        imgUrl: null,
        price: null,
        originalPrice: null,
        detail: null,
        nominal: null,
        type: null,
        sendTime: null,
        courierCompany: null,
        courierNumber: null,
        status: null
      },
      queryAddressParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        number: null,
        userId: null,
        addressId: null,
        commodityId: null,
        couponId: null,
        name: null,
        imgUrl: null,
        price: null,
        originalPrice: null,
        detail: null,
        nominal: null,
        type: null,
        sendTime: null,
        courierCompany: null,
        courierNumber: null,
        status: null
      },
      // 表单参数
      form: {},
      addressForm: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("tb_commodity_type").then(response => {
      this.commodityTypeOptions = response.data;
    });
    this.getDicts("commodity_exchange_status").then(response => {
      this.orderStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询积分兑换商品
列表 */
    getList() {
      this.loading = true;
      listCommodityOrder(this.queryParams).then(response => {
        this.commodityOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        addressId: null,
        commodityId: null,
        couponId: null,
        name: null,
        imgUrl: null,
        price: null,
        originalPrice: null,
        detail: null,
        nominal: null,
        type: null,
        createTime: null,
        sendTime: null,
        courierCompany: null,
        courierNumber: null,
        status: 0
      };
      this.resetForm("form");
    },

    handleAddressShow(row){
      console.log(row)
      this.addressOpen = true;
      getCommodityOrder(row.id).then(response => {
        this.addressForm = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加积分兑换商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCommodityOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改积分兑换商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCommodityOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommodityOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 商品类型字典翻译
    typeFormat(row) {
      return this.selectDictLabel(this.commodityTypeOptions, row.type);
    },
    // 商品状态字典翻译
    statusFormat(row) {
      return this.selectDictLabel(this.orderStatusOptions, row.status);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除积分兑换商品编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCommodityOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有积分兑换商品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCommodityOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
